/*
	xm-uni-storage
	实现对本地数据的增删改查功能
*/

function test(){
	console.log("this is a test");
}

// 日期格式化
// console.log(this.xmU.dateFormat("YYYY-mm-dd HH:MM:SS", new Date));
function dateFormat(fmt, date) {
 let ret;
 const opt = {
	 "Y+": date.getFullYear().toString(),        // 年
	 "m+": (date.getMonth() + 1).toString(),     // 月
	 "d+": date.getDate().toString(),            // 日
	 "H+": date.getHours().toString(),           // 时
	 "M+": date.getMinutes().toString(),         // 分
	 "S+": date.getSeconds().toString()          // 秒
	 // 有其他格式化字符需求可以继续添加，必须转化成字符串
 };
 for (let k in opt) {
	 ret = new RegExp("(" + k + ")").exec(fmt);
	 if (ret) {
		 fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
	 };
 };
 return fmt;
}

// 获取年度统计数据
function getYearTotal(list, time, userIdList){
	var yearTotal = {"in":0, "out":0, "remain":0};
	list.forEach(item => {
		// 有效数据  用户id过滤
		if(item.status === 1 
			&& userIdList.indexOf(item.userId) > -1
			&& dateFormat("YYYY", new Date(item.time)) === dateFormat("YYYY", new Date(time))){
				
			if(item.type === 0){
				yearTotal.out = yearTotal.out + parseFloat(item.money); 
			}else if(item.type === 1){
				yearTotal.in = yearTotal.in + parseFloat(item.money); 
			}
		}
	});
	yearTotal.out = yearTotal.out.toFixed(2);
	yearTotal.in = yearTotal.in.toFixed(2);
	yearTotal.remain = (yearTotal.in - yearTotal.out).toFixed(2);
	return yearTotal;
}
// 获取月度统计数据
function getMonthTotal(list, time, userIdList){
	var monthTotal = {"in":0, "out":0, "remain":0};
	list.forEach(item => {
		// 有效数据  用户id过滤
		if(item.status === 1 
			&& userIdList.indexOf(item.userId) > -1
			&& dateFormat("YYYY-mm", new Date(item.time)) === dateFormat("YYYY-mm", new Date(time))){
				
			if(item.type === 0){
				monthTotal.out = monthTotal.out + parseFloat(item.money); 
			}else if(item.type === 1){
				monthTotal.in = monthTotal.in + parseFloat(item.money); 
			}
		}
	});
	monthTotal.out = monthTotal.out.toFixed(2);
	monthTotal.in = monthTotal.in.toFixed(2);
	monthTotal.remain = (monthTotal.in - monthTotal.out).toFixed(2);
	return monthTotal;
}

// 获取某月的每日数据
function getDayList(list, typeList, time, userIdList){
	var dayList = [];
	list.forEach(item => {
		// 有效数据  用户id过滤
		if(item.status === 1 
			&& userIdList.indexOf(item.userId) > -1
			&& dateFormat("YYYY-mm", new Date(item.time)) === dateFormat("YYYY-mm", new Date(time))){
				
			packageRecord(item, typeList);
			var flag = true;
			dayList.forEach(dayItem => {
				if(dayItem.day === dateFormat("YYYY-mm-dd", new Date(item.time)) ){
					if(item.type === 0){
						dayItem.out += parseFloat(item.money);
					}else if(item.type === 1){
						dayItem.in += parseFloat(item.money);
					}
					dayItem.list.push(item);
					flag = false;
				}
			})
			if(flag){
				var dayRecord = {
					"day": dateFormat("YYYY-mm-dd", new Date(item.time)), 
					"dayName": dateFormat("mm月dd日", new Date(item.time)), 
					"out": 0, 
					"in": 0, 
					"list": []};
				if(item.type === 0){
					dayRecord.out += parseFloat(item.money);
				}else if(item.type === 1){
					dayRecord.in += parseFloat(item.money);
				}
				dayRecord.list.push(item);
				dayList.push(dayRecord);
			}
		}
	});
	// 按日期倒叙排序
	dayList.sort(function(a, b){
		// return a.time<b.time;
		if(a.day > b.day){
			return -1;
		}else if(a.day < b.day){
			return 	1;
		}else{
			return 0;
		}
	
	})
	dayList.forEach(dayItem =>{
		dayItem.in = dayItem.in.toFixed(2);
		dayItem.out = dayItem.out.toFixed(2);
	})
	return dayList;
}

// 补充记录信息的数据（icon/记录名称）
function packageRecord(record, typeList){
	typeList.forEach(item => {
		if(item.code === record.typeCode){
			record.typeName = item.name;
			record.typeIcon = item.icon;
		}
	})
}

// 获取 bar 日推移图
function getBarInfo(list, type, time, userIdList){
	var year = dateFormat("YYYY", time);
	var month = dateFormat("mm", time);
	var dayNum = new Date(year, month, 0).getDate();
	var dayList = [];
	var moneyList = [];
	for(var i = 1; i <= dayNum; i++){
		var barDay = new Date(year, month-1, i);
		var barItem = {
					"day":dateFormat("YYYY-mm-dd", barDay),
					"dayName": dateFormat("mm月dd日", barDay),
					"dayNum":i, 
					"money":0
				};
		list.forEach(item => {
			if(item.status === 1
				&& userIdList.indexOf(item.userId) > -1
				&& item.type === type
				&& dateFormat("YYYY-mm-dd", new Date(item.time)) === barItem.day){
					barItem.money += parseFloat(item.money);
				}
		});
		dayList.push(barItem.dayNum);
		moneyList.push(parseFloat(barItem.money.toFixed(2)) );
	}
	return {"dayList" : dayList, "moneyList" : moneyList};
}

// 获取 bar 日推移图
function getBarInfo2(list, type, time, userIdList, userNameList){
	var dayList = [];
	var moneyList = [];
	userIdList.forEach((item,index) => {
		var tempInfo = getBarInfo(list, type, time, [item])
		dayList = tempInfo.dayList;
		moneyList.push({
						"name": userNameList[index],
						"data": tempInfo.moneyList
					  })
	})
	return {"dayList" : dayList, "moneyList" : moneyList};
}

// 获取 pie 数据
function getPieInfo(list, type, time, userIdList){
	var pieInfo = [];
	var typeInfoList = getTypeInfoList(list, type, time, userIdList);
	typeInfoList.forEach(item =>{
		var pieItem = {"name": item.name, "data": item.data};
		pieInfo.push(pieItem);
	})
	return pieInfo;
}


// 获取分类记录列表
function getTypeInfoList(list, type, time, userIdList){
	var pieList = [];
	list.forEach(item =>{
		if(item.status === 1
			&& userIdList.indexOf(item.userId) > -1
			&& item.type === type
			&& dateFormat("YYYY-mm", new Date(item.time)) === dateFormat("YYYY-mm", new Date(time))){
		
			var pieFlag = true;
			pieList.forEach(pieItem => {
				if(pieItem.typeCode === item.typeCode){
					pieItem.data += parseFloat(item.money);
					pieFlag = false;
				}
			})
			if(pieFlag){
				var pieItem = {
					"name": item.typeName, 
					"type": type, 
					"typeCode": item.typeCode, 
					"typeIcon": item.typeIcon, 
					"data": parseFloat(item.money), 
					};
				pieList.push(pieItem);
			}
		}
	});
	// pieList按数据大小排序
	pieList.sort(function(a, b){
		if(a.data > b.data){
			return -1;
		}else if(a.data < b.data){
			return 	1;
		}else{
			return 0;
		}
	})
	pieList.forEach(pieItem =>{
		pieItem.data = parseFloat(pieItem.data.toFixed(2));
	})
	return pieList;
}

// 获取年度统计
function getMonthList(list, time, userIdList){
	var monthList = [];
	var year = dateFormat("YYYY", time);
	for(var i=1; i<=12; i++){
		var month = new Date(year, i, 0);
		var monthItem = {"month": dateFormat("YYYY-mm", month), "monthName":dateFormat("mm月", month),  "in": 0, "out" : 0, "remain":0};
		list.forEach(item => {
			if(item.status === 1
				&& userIdList.indexOf(item.userId) > -1
				&& dateFormat("YYYY-mm", new Date(item.time)) === monthItem.month ){
				
				// 月统计
				if(item.type === 0){
					monthItem.out += parseFloat(item.money);
				}else if(item.type === 1){
					monthItem.in += parseFloat(item.money)
				}
			}
		});
		monthItem.in = monthItem.in.toFixed(2);
		monthItem.out = monthItem.out.toFixed(2);
		monthItem.remain = (monthItem.in - monthItem.out).toFixed(2);
		monthList.push(monthItem);
	}
	return monthList;
}

// 获取明细
function getTatil(list, type, time, code, userIdList, userNameList){
	var tatilList = [];
	list.forEach(item => {
		if(item.status === 1
			&& item.type === type
			&& item.typeCode === code
			&& userIdList.indexOf(item.userId) > -1
			&& dateFormat("YYYY-mm", new Date(item.time)) === dateFormat("YYYY-mm", time) ){
				item.userName = userNameList[userIdList.indexOf(item.userId)];
				item.dayName = dateFormat("mm月dd日", new Date(item.time));
				tatilList.push(item);
		}
	});
	// 按日期倒叙排序
	tatilList.sort(function(a, b){
		// return a.time<b.time;
		if(a.time > b.time){
			return -1;
		}else if(a.time < b.time){
			return 	1;
		}else{
			return 0;
		}
	
	})
	return tatilList;
}
			
			
export default {
	dateFormat,
	getYearTotal,
	getMonthTotal,
	getDayList,
	getBarInfo,
	getPieInfo,
	getTypeInfoList,
	getMonthList,
	getBarInfo2,
	getTatil,
	test
}